//
//  DDZdtjDetailVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/8/15.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDZdtjDetailVC.h"
#import "DDNetWorkAPIClient.h"
#import "DDTJ.h"
#import "DDZdtjDetailContentVC.h"
@interface DDZdtjDetailVC ()<ViewPagerDataSource, ViewPagerDelegate>


@end

@implementation DDZdtjDetailVC

- (void)viewDidLoad {
   
    
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    self.navigationController.navigationBarHidden = NO;
    self.title = @"人数详情";
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    //self.navigationController.navigationBar.hidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_Background"] forBarMetrics:UIBarMetricsDefault];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [back setFrame:CGRectMake(5,7, 22, 30)];
    [back setImage:[UIImage imageNamed:@"top_返回"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = barButton;
    self.navigationController.navigationBar.hidden = NO;
    
    sleep(0.5);
    
    self.dataSource = self;
    self.delegate = self;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //[self createUI];
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
}
-(void)popVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createData
{
    //_datas = [NSMutableDictionary dictionary];
    
    
    
}
//- (void)subdata:(NSString *)result
//{
//    if ([result integerValue] > 2) {
//    
//        [self process];
//        return;
//    }
//    __weak DDZdtjDetailVC *weakSelf = self;
//    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
//    [network mainAnswerUser:@{@"topicId":_topicId,@"qid":_qid,@"result":result} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
//        NSLog(@"%@",object);
//        if (object) {
//            DDTJ *tj = (DDTJ *)object;
//            [weakSelf.datas setObject:tj.data forKey:result];
//            
//            [weakSelf subdata:[NSString stringWithFormat:@"%d",[result integerValue]+1]];
//            
//            
//        }
//    }];
//
//}
//
//- (void)process
//{
//    
//    NSLog(@"%@",_datas);
//   
//   
//}


#pragma mark - ViewPagerDataSource
- (NSUInteger)numberOfTabsForViewPager:(ViewPagerController *)viewPager {
    if (_datas.count == 1) {
        return 1;
    }
    if (_isHasAnswer) {
        return 3;
    }else{
        return 2;
    }
    
    return _datas.count;
}
- (UIView *)viewPager:(ViewPagerController *)viewPager viewForTabAtIndex:(NSUInteger)index {
    
    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:20.0];
    
    if (_datas.count == 1) {
        label.text = @"选项";
    }
    else{
    if (_isHasAnswer) {
        switch (index) {
            case 0:
                label.text = @"正确";
                break;
            case 1:
                label.text = @"错误";
                break;
            case 2:
                label.text = @"未作答";
                break;
            default:
                break;
        }

    }else{
        switch (index) {
            case 0:
                label.text = @"已作答";
                break;
            case 1:
                label.text = @"未做答";
                break;
            default:
                break;
        }

    }
    }
    
    
    
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:42.0/255.0 green:172.0/255.0 blue:226.0/255.0 alpha:1.0];
    [label sizeToFit];
    
    return label;
}

- (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
    
    if (_datas.count == 1) {
        NSArray *array = [_datas objectForKey:@"选项"];
        DDZdtjDetailContentVC *cvc = [[DDZdtjDetailContentVC alloc] initWithNibName:@"DDZdtjDetailContentVC" bundle:nil];
        cvc.array = array;
         return cvc;
    }else{
    
    int order = 0;
    if (_isHasAnswer) {
        if (index == 0) {
            order = 1;
        }
        else if (index == 1) {
            order = 2;
        }
        else if (index == 2)
        {
            order = 0;
        }else if(index == 3)
        {
            order = 3;
        }
    }else{
        if (index == 1) {
            order = 0;
        }
        else if (index == 0) {
            order = 3;
        }
    }
    
    
   NSArray *array = [_datas objectForKey:[NSString stringWithFormat:@"%d",order]];
   DDZdtjDetailContentVC *cvc = [[DDZdtjDetailContentVC alloc] initWithNibName:@"DDZdtjDetailContentVC" bundle:nil];
    cvc.array = array;
         return cvc;
    }
   
}

#pragma mark - ViewPagerDelegate
- (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value {
    
    int num = 3;
    if (!_isHasAnswer) {
        num = 2;
    }
    if (_datas.count == 1) {
        num = 1;
    }
    
    switch (option) {
        case ViewPagerOptionStartFromSecondTab:
            return 0.0;
            break;
        case ViewPagerOptionCenterCurrentTab:
            return 0.0;
            break;
        case ViewPagerOptionTabLocation:
            return 1.0;
            break;
        case ViewPagerOptionTabWidth:
            
            return  [[UIApplication sharedApplication].delegate window].frame.size.width/num;
            break;
        case ViewPagerOptionTabOffset:
            return 0;
            break;
        default:
            break;
    }
    
    return value;
}
- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color {
    
    switch (component) {
        case ViewPagerIndicator:
        return [[UIColor colorWithRed:42.0/255.0 green:172.0/255.0 blue:226.0/255.0 alpha:1.0] colorWithAlphaComponent:1.0];
            break;
        default:
            break;
    }
    
    return color;
}

-(void)switchVC:(NSNotification *)note
{
    UIViewController *vc = note.object;
    [self presentViewController:vc animated:YES completion:nil];
    //[self.navigationController pushViewController:vc animated:YES];
}
@end
